package huu.phong.youtube;

import java.net.HttpURLConnection;
import java.net.URL;

public final class Utils {
	
	public static int[] ERROR_CODE = {403};
	
	public static boolean isURLAvailable(String link){
		boolean ret = false;
		try {
			URL url = new URL(link);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//			connection.setInstanceFollowRedirects(false);
			connection.setRequestMethod("HEAD");
			connection.connect();
			if (isErrorCode(connection.getResponseCode())) {
				ret = false;
			} else {
				ret = true;
			}
			connection.disconnect();
		} catch (Exception e) {
			ret = false;
		}
		return ret;
	}
	
	private static boolean isErrorCode(int code){
		boolean ret = false;
		for (int i = 0; i < ERROR_CODE.length; i++) {
			if (code == ERROR_CODE[i]) {
				ret = true;
				break;
			}
		}
		if (code >= 400) ret = true;
		return ret;
	}
}
